package com.zgt.web.action;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.zgt.model.Research;
import com.zgt.service.ResearchService;
import com.zgt.util.MD5;
import com.zgt.util.Page;
import com.zgt.web.AbstractAction;

@Controller("researchAction")
@Scope("prototype")
public class ResearchAction extends AbstractAction implements
		ModelDriven<Research> {

	private String key;

	private String keyType;
	private MD5 m = new MD5();
	private Page page = new Page();
	private Map request = (Map) ActionContext.getContext().get("request");
	private Research research = new Research();

	private ResearchService researchService;
	private Map session = ActionContext.getContext().getSession();
	private String submitFlag;

	public String add() {
		if (submitFlag == null) {
			return INPUT;
		}
		researchService.addResearch(research);
		request.put("url", "research_list.action");
		return SUCCESS;
	}

	public String delete() {
		researchService.deleteResearchById(research.getId());
		return SUCCESS;
	}

	public String edit() {
		if (submitFlag == null) {
			research = researchService.getResearchById(research.getId());
			request.put("research", research);
			return INPUT;
		}
		researchService.updateResearch(research);
		request.put("url", "research_list.action");
		return SUCCESS;
	}
	
	public String list() {
		request.put("page", researchService.listResearch(page));
		return INPUT;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	public String getKey() {
		return key;
	}

	public String getKeyType() {
		return keyType;
	}

	public Research getModel() {
		return research;
	}

	public Page getPage() {
		return page;
	}

	public String getSubmitFlag() {
		return submitFlag;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public void setKeyType(String keyType) {
		this.keyType = keyType;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	@Resource
	public void setresearchService(ResearchService researchService) {
		this.researchService = researchService;
	}

	public void setSubmitFlag(String submitFlag) {
		this.submitFlag = submitFlag;
	}

	@Override
	public void validate() {
		if (submitFlag == null) {
			return;
		}
		if ("upload".equals(submitFlag)) {
			return;
		}
	}

}
